package com.atom.module.mvi.binder.connector

import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Observable.wrap
import io.reactivex.rxjava3.core.ObservableSource

/**
 * rxjava 一定得学通
 */
internal class NotNullConnector<Out, In>(private val mapper: (Out) -> In?) :
    Connector<Out, In> {
    override fun invoke(element: ObservableSource<out Out>): ObservableSource<In> =
        // wrap 将源包装,通过flatMap进行转换,转换的真是策略是将源的发出的类型A 转换为 类型B
        wrap(element)
            .flatMap {
                mapper(it)
                    ?.let { value -> Observable.just(value) }
                    ?: Observable.empty()
            }

    override fun toString(): String = mapper.toString()
}
